<?php
    
    class Model_Friend extends Joob_Db_Table {
        public $id;
        public $user_id1; //who send the invitation
        public $user_id2;
        public $since;
        public $msg;
        public $state;
        
        protected $key = "id";
	
		protected $db = "friends";
        
        public function addFriend()	{
        	$this->setOption('since',time());
        	if($this->insert('user_id1,user_id2,state,since,msg')){
        		return true;
        	}
        	else{
        		return false;
        	}
        }
        
    	public function isInFriend()	{
        	$f=$this->search('(user_id1='.$this->user_id1.' and user_id2='.$this->user_id2.') or (user_id1='.$this->user_id2.' and user_id2='.$this->user_id1.')');
        	if(sizeof($f)>0){
        		return $f[0];
        	}
        	return false;
        }
        
        public function isInvitingFriend()	{
        	$f=$this->search('state=0 and user_id1='.$this->user_id1.' and user_id2='.$this->user_id2);
        	if(sizeof($f)>0){
        		return true;
        	}
        	return false;
        }
        
    	public function isFriend()	{
        	$f=$this->search('state=1 and ((user_id1='.$this->user_id1.' and user_id2='.$this->user_id2.') or (user_id1='.$this->user_id2.' and user_id2='.$this->user_id1.'))');
        	if(sizeof($f)>0){
        		return true;
        	}
        	return false;
        }
    }
?>